<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!--父组件模板-->
<div id="app">
<!--  父组件监听事件-->
  <cpn @item-click="cpnClick"></cpn>
</div>
<template id="cpn">
  <div>
    <button v-for="item in categories" @click="btnClick(item,$event)">{{item.name}}</button>
  </div>
</template>
<script src="../vue.js"></script>
<script>
  // 子组件
  const cpn = {
    template: `#cpn`,
    data(){
      return{
        categories:[
          {
            id: 'aaa',
            name: '热门推荐'
          },
          {
            id:'bbb',
            name:'手机数码'
          },
          {
            id:'ccc',
            name:'家具家电'
          },          {
            id:'ddd',
            name:'电脑办公'
          }
        ]
      }
    },
    methods:{
      btnClick(item,event){
        //子组件发射事件
        this.$emit('item-click',item)
      }
    }
  }

  // 父组件
  const app = new Vue({
    el: '#app',
    data: {
      message: 'hello'
    },
    components: {
      cpn
    },
    methods:{
      cpnClick(item){
        console.log(item)
      }
    }

  })
</script>
</body>
</html>
